package com.frank.service.test3;

import java.nio.ByteBuffer;

import static com.frank.util.ByteBufferUtil.debugAll;

/**
 * @Author: huweifeng
 * @Date: 2024/12/6
 * @Description: 测试ByteBuffer读写过程
 **/
public class TestByteBufferReadWrite {
    public static void main(String[] args) {
        ByteBuffer buffer = ByteBuffer.allocate(10);
        buffer.put((byte)0x61); // 'a'
        debugAll(buffer);
        buffer.put(new byte[]{0x62,0x63,0x64}); // b c d
        debugAll(buffer);

        buffer.flip(); // 切换为读模式
        System.out.println("读到的字符：" + (char)buffer.get()); // a
        debugAll(buffer);

        buffer.compact();
        debugAll(buffer);
        buffer.put(new byte[]{0x65, 0x66}); // e f
        debugAll(buffer);
    }
}
